import java.io.IOException;

import java.rmi.RemoteException;

//import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import vlms.bean.Account;
import vlms.bean.Person;
import vlms.bean.PremiumCustomer;
import vlms.bean.SimpleCustomer;

import vlms.service.VLMSServiceProxy;
//import connection.ServiceProxy;
/**
 * Servlet implementation class signIn
 */
public class signIn extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public signIn() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		HttpSession session; 
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		String destination = null;
		String sPersonId;
		
		Account oAccount = new vlms.bean.Account();
		
		oAccount.setUsername(username);
		oAccount.setPassword(password);
		try {
			VLMSServiceProxy oProxy = new VLMSServiceProxy(); 
			oAccount = oProxy.logIn(oAccount);
		
			sPersonId = String.valueOf(oAccount.getPersonid());
			Person oPerson = oProxy.retrievePersonById(sPersonId);
			
			session = request.getSession(true);
			if(oPerson.getMembertype().equals("SC") || oPerson.getMembertype().equals("PC")) {
				if(oPerson.getMembertype().equals("PC")) {
					PremiumCustomer oPC = oProxy.retrievePremiumCustomerByPersonId(sPersonId);
					session.setAttribute("PremiumCustomer", oPC);
					session.setAttribute("MemberType", oPerson.getMembertype());
					destination = "/home";
				}
				else if(oPerson.getMembertype().equals("SC")) {
					SimpleCustomer oSC = oProxy.retrieveSimpleCustomerByPersonId(sPersonId);
					session.setAttribute("SimpleCustomer", oSC);
					session.setAttribute("MemberType", oPerson.getMembertype());
					destination = "/home";
				}
				
				session.setAttribute("Account", oAccount);
				session.setAttribute("Person", oPerson);
				session.setAttribute("authenticated", "true");
				
				System.out.println("destination" + destination);
				request.setAttribute("Message", "Welcome To Video Library System");
			}
			else {
				request.setAttribute("message", "Invalid Username/Password");
				destination = "/signIn.jsp";
			}
			
		}
		catch(RemoteException re) {
			request.setAttribute("message", re.getMessage());
			destination = "/signIn.jsp";
			//re.printStackTrace();
		}
		
		
		response.sendRedirect(request.getContextPath() + destination);
		
	}

}
